package com.lottery.ruok.lottrey.ui.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;

/**
 * Created by ASUS on 2017/9/13.
 */

public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {
    private boolean canListScroll = false;
    private View footerView;
    private boolean isHaveMoreData = true;// 是否有更多数据(默认为有)
    private ProgressBar progressBar;
    private TextView tipContext;
    private boolean isLoading = false;// 是否正在加载
    private float lastY;
    private OnLoadMoreListener mOnLoadMoreListener;

    public void setCanListScroll(boolean canListScroll) {
        this.canListScroll = canListScroll;
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //动态载入底部布局
        footerView = LayoutInflater.from(context).inflate(
                R.layout.layout_listview_footerview, null);
        progressBar = footerView.findViewById(R.id.footer_progressbar);
        tipContext = footerView.findViewById(R.id.footer_hint_textview);
        //向listView的底部添加布局(此时当给listView设置Item点击事件的时候，默认不触发这个添加的布局的点击事件)
        footerView.setVisibility(INVISIBLE);
        addFooterView(footerView, null, false);
        setOnScrollListener(this);
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (canListScroll) {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                getParent().getParent().requestDisallowInterceptTouchEvent(true);
            } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
                if (lastY > ev.getY()) {
                    // 如果是向上滑动，且不能滑动了，则让ScrollView处理
                    if (!canScrollList(1)) {
                        getParent().getParent().requestDisallowInterceptTouchEvent(false);
                        return false;
                    }
                } else if (ev.getY() > lastY) {
                    // 如果是向下滑动，且不能滑动了，则让ScrollView处理
                    if (!canScrollList(-1)) {
                        getParent().getParent().requestDisallowInterceptTouchEvent(false);
                        return false;
                    }
                }
            }
            lastY = ev.getY();
            return super.dispatchTouchEvent(ev);
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

    /**
     * 设置是否还有更多数据
     *
     * @param isHaveMoreData
     */
    public void setHaveMoreData(boolean isHaveMoreData) {
        this.isHaveMoreData = isHaveMoreData;
        if (!isHaveMoreData) {
            tipContext.setText("只有这么多啦");
            progressBar.setVisibility(View.GONE);
        } else {
            tipContext.setText("正在加载");
            progressBar.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 加载完成
     */
    public void onLoadComplete() {
        isLoading = false;
    }

    public boolean isLoading() {
        return isLoading;
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (footerView.getVisibility() != VISIBLE) {
            footerView.setVisibility(VISIBLE);
        }
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
            if (view.getLastVisiblePosition() == view.getCount() - 1 && !isLoading && mOnLoadMoreListener != null && isHaveMoreData) {
//                isLoading = true;
                mOnLoadMoreListener.onLoadMore();
            }
        }
    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        if (mOnLoadMoreListener != null) {
            mOnLoadMoreListener.onScroll(i);
        }
    }

    /**
     * 加载更多的监听
     */
    public interface OnLoadMoreListener {
        void onLoadMore();

        void onScroll(int index);
    }

    /**
     * 设置加载监听
     *
     * @param mOnLoadMoreListener
     */
    public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
        this.mOnLoadMoreListener = mOnLoadMoreListener;
    }
}
